package com.appcenter.application.service;

import com.appcenter.application.dto.RoleUserDto;
import com.appcenter.application.dto.Users.UserAddrDto;
import com.appcenter.application.input.User.*;
import com.appcenter.application.mapper.OrgUsersMapper;
import com.appcenter.application.mapper.RoleUsersMapper;
import com.appcenter.application.mapper.UserAddrsMapper;
import com.appcenter.application.mapper.UsersMapper;
import com.appcenter.application.model.UserAddrs;
import com.appcenter.application.model.Users;
import com.appcenter.application.output.User.*;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserService {
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    UserAddrsMapper userAddrsMapper;


    @Autowired
    OrgUsersMapper orgUsersMapper;

    @Autowired
    RoleUsersMapper roleUsersMapper;


    public CreateUserOutput CreateUser(CreateUserInput input) throws JsonProcessingException {
        CreateUserOutput output = new CreateUserOutput();

        Users UserCode = usersMapper.getUsersByUserCode(input.getItem().getUserCode());
        if (UserCode != null) {
            throw new ErrorJsonResult("1000083", "用户账号已存在！");
        }
        Users user = new Users();
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();

        user.setId(uuid);
        user.setUserCode(input.getItem().getUserCode());
        user.setUserName(input.getItem().getUserName());
        user.setPassword(input.getItem().getPassword());
        user.setDingUserId(input.getItem().getDingUserId());
        user.setEmail(input.getItem().getEmail());
        user.setAccountIsDisabled(input.getItem().getAccountIsDisabled());
        user.setPhone(input.getItem().getPhone());
        user.setSex(input.getItem().getSex());
        user.setBirthday(input.getItem().getBirthday());
        user.setOfficePhone(input.getItem().getOfficePhone());
        user.setJobNumber(input.getItem().getJobNumber());
        user.setCardNo(input.getItem().getCardNo());
        if (input.getItem().getUserAddress() != null) {
            UserAddrDto curUserAddr = new UserAddrDto();
            curUserAddr.setId(input.getItem().getUserAddress().getProvinceCode() + "," + input.getItem().getUserAddress().getCityCode() + "," + input.getItem().getUserAddress().getAreaCode());
            curUserAddr.setName(input.getItem().getUserAddress().getProvinceName() + "," + input.getItem().getUserAddress().getCityName() + "," + input.getItem().getUserAddress().getAreaName());
            user.setCityCode(objectMapper.writeValueAsString(curUserAddr));
        }
        //添加用户信息
        usersMapper.insertSelective(user);
        if (input.getItem().getUserAddress() != null) {
            UserAddrs userAddrs = userAddrsMapper.getUserAddsByUserId(user.getId());
            if (userAddrs != null) {
                userAddrsMapper.deleteByPrimaryKey(userAddrs.getId());
            }
            UserAddrs curUserAddr = new UserAddrs();
            curUserAddr.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
            curUserAddr.setUserId(user.getId());
            curUserAddr.setProvinceCode(input.getItem().getUserAddress().getProvinceCode());
            curUserAddr.setCityCode(input.getItem().getUserAddress().getCityCode());
            curUserAddr.setAreaCode(input.getItem().getUserAddress().getAreaCode());
            userAddrsMapper.insertSelective(curUserAddr);
            user.setUserAddress(curUserAddr);
        }
        output.setItem(user);
        return output;
    }

    public GetDataUsersOutput GetDataUsers(GetDataUsersInput input) {
        GetDataUsersOutput output = new GetDataUsersOutput();
        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "UserCode");
        List<Users> ls = usersMapper.searchUsers(input);
        PageInfo<Users> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Users>(page.getList(), (int) page.getTotal()));
        return output;
    }

    public PageLoadOutput PageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(GetDataUsers(input).getItems());
        return output;
    }

    public EditUserOutput EditUser(EditUserInput input) throws JsonProcessingException {
        EditUserOutput output = new EditUserOutput();
        Users UserCode = usersMapper.getUsersByUserCodeNoId(input.getItem().getUserCode(), input.getItem().getId());
        if (UserCode != null) {
            throw new ErrorJsonResult("1000083", "用户账号已存在！");
        }


        Users oldUser = usersMapper.getUserById(input.getItem().getId());
        oldUser.setUserCode(input.getItem().getUserCode());
        oldUser.setUserName(input.getItem().getUserName());
        oldUser.setPassword(input.getItem().getPassword());
        oldUser.setDingUserId(input.getItem().getDingUserId());
        oldUser.setEmail(input.getItem().getEmail());
        oldUser.setAccountIsDisabled(input.getItem().getAccountIsDisabled());
        oldUser.setPhone(input.getItem().getPhone());
        oldUser.setSex(input.getItem().getSex());
        oldUser.setBirthday(input.getItem().getBirthday());
        oldUser.setOfficePhone(input.getItem().getOfficePhone());
        oldUser.setJobNumber(input.getItem().getJobNumber());
        oldUser.setCardNo(input.getItem().getCardNo());

        if (input.getItem().getUserAddress() != null) {
            UserAddrDto curUserAddr = new UserAddrDto();
            curUserAddr.setId(input.getItem().getUserAddress().getProvinceCode() + "," + input.getItem().getUserAddress().getCityCode() + "," + input.getItem().getUserAddress().getAreaCode());
            curUserAddr.setName(input.getItem().getUserAddress().getProvinceName() + "," + input.getItem().getUserAddress().getCityName() + "," + input.getItem().getUserAddress().getAreaName());
            oldUser.setCityCode(objectMapper.writeValueAsString(curUserAddr));
        }

        //修改用户信息
        usersMapper.updateByPrimaryKey(oldUser);
        if (input.getItem().getUserAddress() != null) {
            UserAddrs userAddrs = userAddrsMapper.getUserAddsByUserId(oldUser.getId());
            if (userAddrs != null) {
                userAddrsMapper.deleteByPrimaryKey(userAddrs.getId());
            }
            UserAddrs curUserAddr = new UserAddrs();
            curUserAddr.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
            curUserAddr.setUserId(oldUser.getId());
            curUserAddr.setProvinceCode(input.getItem().getUserAddress().getProvinceCode());
            curUserAddr.setCityCode(input.getItem().getUserAddress().getCityCode());
            curUserAddr.setAreaCode(input.getItem().getUserAddress().getAreaCode());
            userAddrsMapper.insertSelective(curUserAddr);
            oldUser.setUserAddress(curUserAddr);
        }
        output.setItem(oldUser);
        return output;
    }

    public DeleteUserOutput DeleteUser(DeleteUserInput input) {
        DeleteUserOutput output = new DeleteUserOutput();
        //删除部门的用户信息
        orgUsersMapper.deleteOrgUserByUserId(input.getItem().getId());
        //删除角色的用户信息
        roleUsersMapper.deleteRoleUserByUserId(input.getItem().getId());
        //删除用户信息
        usersMapper.deleteByPrimaryKey(input.getItem().getId());
        //删除用户的地址信息
        userAddrsMapper.deleteUserAddrByUserId(input.getItem().getId());
        return output;
    }

    public ChangeAccountStatusOutput ChangeAccountStatus(ChangeAccountStatusInput input) {
        ChangeAccountStatusOutput output = new ChangeAccountStatusOutput();

        Users user = null;
        if (input.getId() != null && input.getId().length() > 0) {
            user = usersMapper.getUserById(input.getId());
        } else if (input.getUserCode() != null && input.getUserCode().length() > 0) {
            user = usersMapper.getUserByUserCode(input.getUserCode());
        }
        if (user == null) {
            throw new ErrorJsonResult("1000084", "找不到用户！");
        }
        user.setAccountIsDisabled(input.getAccountIsDisabled());
        usersMapper.updateByPrimaryKey(user);
        output.setItem(user);
        return output;
    }

    public ResetPassWordOutput ResetPassWord(ResetPassWordInput input) {
        ResetPassWordOutput output = new ResetPassWordOutput();
        Users  user = usersMapper.getUserById(input.getUserId());
        if (user == null) {
            throw new ErrorJsonResult("1000084", "找不到用户！");
        }
        user.setPassword("123456");
        usersMapper.updateByPrimaryKey(user);
        output.setResult("成功");
        return output;
    }
}
